$(document).ready
(
	function()
	{
		if(!IsLoggedOn) showLogonScreen();
		else populateCurrentPage();

		$(".diary-control-arrow").click(arrowClick);
		

/*		alert("Is logged on = " + IsLoggedOn);
		alert("Current page = " + CurrentPage);*/
	}
	
//load	
);

function showLogonScreen()
{
	$("#logon").slideDown(2000);
	$("#logon-button").click(tryLogon);
	$("#logon-username").keyup(function(event){if(event.which==13)tryLogon();});
	$("#logon-password").keyup(function(event){if(event.which==13)tryLogon();});
	$("#logon-button").hover(function(){$(this).addClass("logon-button-hover");},function(){$(this).removeClass("logon-button-hover");});
	$("#logon-password-text").hover(function(){$(this).addClass("logon-password-text-hover");},function(){$(this).removeClass("logon-password-text-hover");});
	$("#logon-password-text").click(showPasswordRequest);
	$("#logon-password-request").hover(function(){$(this).addClass("logon-password-request-hover");},function(){$(this).removeClass("logon-password-request-hover");});
	$("#logon-password-request").click(makePasswordRequest);


	$("#menu-admin").hide();
	$("#menu-logoff").hide();
}

function tryLogon()
{
//	alert ("try to logon");
	$("#logon-message").slideUp(1000);
//	$("#login-dummy").load('model/ajax/check-credentials.php','UserName='+$("#logon-username").val()+'&Password='+$("#logon-password").val()+'&SessionID='+SessionID+'&UserID='+UserID,tryLogonCallback)

	$.post
	('model/ajax/check-credentials.php',
		{
			UserName:$("#logon-username").val(),
			Password:$("#logon-password").val()

		},
		tryLogonCallback
	);	
}

function tryLogonCallback(data)
{
	var arr = data.split(",");
	IsLoggedOn=(arr[0]=="1")?1:0;
	UserID=arr[1];
	SessionID=arr[2];
	
//	alert (IsLoggedOn);

	if(IsLoggedOn)
	{
		$("#logon").slideUp(2000,function(){$("#logon-message").html("");});
		populateCurrentPage();
	}
	else
	{
		$("#logon-message").html("Login Incorrect - try again");
		$("#logon-message").slideDown(800);
	}
}

function showPasswordRequest()
{
	$("#logon-password-user").val("");
	$("#logon-password-hidden-text").show();
	$("#logon-password-text").hide();
}

function makePasswordRequest()
{
	$("#logon-password-hidden-text").hide();
	$("#logon-password-text").show();
	$.post
	('model/ajax/request-password.php',
		{
			UserID:$("#logon-password-user").val(),
		},
		tryPasswordRequestCallback
	);	
	
}

function tryPasswordRequestCallback(data)
{
	if (data != "")
	{
		alert(data);
	}
}

function getCurrentUserInfo()
{
//	alert(	"SessionID = " + SessionID + "UserID = " + UserID); 

	
	$.post
	('model/ajax/get-current-user-info.php',
		{
			UserID:UserID,
			SessionID:SessionID,
		},
		populateCurrentPage1
	);	

}


function populateCurrentPage()
{
	$("#menu-diary").click(function(){loadNewPage("views/diary/diary.php");});
	$("#menu-reports").click(function(){loadNewPage("views/reports/reports.php");});
	$("#menu-messaging").click(function(){loadNewPage("views/messaging/messaging.php");});
	$("#menu-admin").click(function(){loadNewPage("views/admin/admin.php");});
	$("#menu-logoff").click(function(){IsLoggedOn=0;UserID=-1;SessionID=-1;loadNewPage("views/diary/diary.php");});
	if(UserID==1)$("#menu-admin").show();else $("#menu-admin").hide();
	$("#menu-logoff").show();
	$(".menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	getCurrentUserInfo();
}

function populateCurrentPage1(data)
{
//	alert(data);

	CurrentUser = JSON.parse(data);
//	alert (CurrentUser['LongName']);
//	alert(CurrentUser['ShortName']);
//	alert ("Hello");

	
	$("#welcome").text("Welcome "+CurrentUser['LongName']+" - You have "+CurrentUser['Group']+" rights.");

	switch (CurrentPage)
	{
		case 'views/diary/diary.php':
		default:
			loadDiary();
			break;
			
		case 'views/reports/reports.php':
			loadReports();
			break;
			
		case 'views/messaging/messaging.php':
			loadMessaging();
			break;
			
		case 'views/admin/admin.php':
			loadAdmin();
			break;
	}
}

function loadNewPage(pageURL)
{
	$("#new-page-form-page").val(pageURL); 
	$("#new-page-form-SessionID").val(SessionID); 
	$("#new-page-form-UserID").val(UserID); 
	$("#new-page-form").submit();
}